package com.haoyi.feignapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;

import com.haoyi.feignapi.entity.erp.ErpOCC;
import com.haoyi.feignapi.entity.ocm.ClientOCM;
import com.haoyi.feignapi.entity.ocm.EfficacyOCM;
import com.haoyi.feignapi.entity.ocm.HbaOCM;
import com.haoyi.feignapi.mapper.ClientOCMMapper;
import com.haoyi.feignapi.mapper.EfficacyOCMMapper;
import com.haoyi.feignapi.mapper.HbaMapper;
import com.haoyi.feignapi.mapper.OcmMapper;
import com.haoyi.feignapi.service.OcmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.math.BigDecimal;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
@DS("ocm")
public class OcmServiceImpl implements OcmService {

    final
    OcmMapper ocmMapper;

    final
    ClientOCMMapper clientOCMMapper;

    final
    HbaMapper hbaMapper;

    final
    EfficacyOCMMapper efficacyOCMMapper;

    final
    PlatformTransactionManager transactionManager;

    public OcmServiceImpl(OcmMapper ocmMapper, PlatformTransactionManager transactionManager, ClientOCMMapper clientOCMMapper, EfficacyOCMMapper efficacyOCMMapper, HbaMapper hbaMapper) {
        this.ocmMapper = ocmMapper;
        this.transactionManager = transactionManager;
        this.clientOCMMapper = clientOCMMapper;
        this.efficacyOCMMapper = efficacyOCMMapper;
        this.hbaMapper = hbaMapper;
    }


    @Override
    public Map<String, Object> getCag(Map<String, Object> paramMap) {
        Map<String, Object> resultMap = ocmMapper.getCag(paramMap);
        if (resultMap == null) {
            resultMap = new HashMap<>();
        }
        String field1 = Optional.ofNullable(resultMap.get("CAG008"))
                .map(Object::toString)
                .orElse("");
        String field2 = Optional.ofNullable(resultMap.get("CAGCLASS"))
                .map(Object::toString)
                .orElse("");
        String field3 = Optional.ofNullable(resultMap.get("CAGUNIT"))
                .map(Object::toString)
                .orElse("");
        resultMap.put("CAG008", field1);
        resultMap.put("CAGCLASS",field2);
        resultMap.put("CAGUNIT",field3);
        return resultMap;
    }

    @Override
    public Map<String, Object> getDan(Map<String, Object> paramMap) {
        String daechnl = ocmMapper.getDaechnl(paramMap.get("erp_code").toString());
        paramMap.put("dan002", daechnl);
        return ocmMapper.getDan(paramMap);
    }


    @Override
    public Map<String, Object> insertOcmOrder(Map<String, Object> paramMap) {
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Map<String, Object> header = (Map<String, Object>) paramMap.get("header");
            ocmMapper.insertOrderHeader(header);
            List<Map<String, Object>> detail_list = (List<Map<String, Object>>) paramMap.get("detail_list");
            for (Map<String, Object> detail : detail_list) {
                ocmMapper.insertOrderDetail(detail);
            }
            transactionManager.commit(status);
            resultMap.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(status);
            resultMap.put("status", "error");
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> checkErpCode(Map<String, Object> paramMap) {
        Integer selectNum = ocmMapper.checkErpCode(paramMap);
        Map<String, Object> resultMap = new HashMap<>();
        if (selectNum > 0) {
            resultMap.put("status", "success");
        } else {
            resultMap.put("status", "error");
            resultMap.put("message", "Erp客户编号校验失败");    
        }

        return resultMap;
    }

    @Override
    public int insertOCM(ErpOCC erpOCC) {
        ClientOCM clientOCM = new ClientOCM();
        clientOCM.setDae014("0");
        clientOCM.setDae015("N");
        clientOCM.setDae002("2");
        clientOCM.setDae005("2");
        clientOCM.setDaecorg("100001");
        clientOCM.setDae003("1");
        clientOCM.setDaecurr("RMB");
        clientOCM.setDaeaper("DR00804");
        clientOCM.setDaecper("DR00804");
        clientOCM.setDaemper("DR00804");
        clientOCM.setDae010("KC47");
        clientOCM.setDaelorg("100001");
        clientOCM.setDaetype("1");
        clientOCM.setDaechnl("12");
        clientOCM.setDaelock(BigDecimal.valueOf(1));
        clientOCM.setDae012("N");
        clientOCM.setDaeent(662);
        clientOCM.setDae016("N");
        clientOCM.setDaetax("S013");
        clientOCM.setDaecdept("100001");
        clientOCM.setDaestus("Y");
        clientOCM.setDae013("N");
        clientOCM.setDae017("2");
        clientOCM.setDae036("1");
        clientOCM.setDae033(BigDecimal.valueOf(100));
        clientOCM.setDae037("101");
        clientOCM.setDaetobj("1");
        clientOCM.setDae053("Y");
        clientOCM.setDaeustus("0");
        clientOCM.setDaeuerp("Y");
        clientOCM.setDae061("N");
        clientOCM.setDae063("3");
        clientOCM.setDae090("3");
        clientOCM.setDae092("N");
        clientOCM.setDaebdept(erpOCC.getGen03());
        clientOCM.setDaebper(erpOCC.getOcc04());
        clientOCM.setDaecustN1(erpOCC.getOcc02());
        clientOCM.setDaeaobj(erpOCC.getOcc01());
        clientOCM.setDaecust(erpOCC.getOcc01());
        clientOCM.setDae001N1(erpOCC.getOcc02());
        int cc = clientOCMMapper.insert(clientOCM);
        EfficacyOCM efficacyOCM = new EfficacyOCM();
        efficacyOCM.setDafstus("Y");
        efficacyOCM.setDafent(662);
        efficacyOCM.setDaftype("1");
        efficacyOCM.setDaflorg("100001");
        efficacyOCM.setDafcust(erpOCC.getOcc01());
        int ee = efficacyOCMMapper.insert(efficacyOCM);
        if (cc==0 || ee==0){
            return 0;
        }else if (cc==1 && ee==1){
            return 1;
        }
        return 2;
    }

    @Override
    public int insert(HbaOCM record) {
        int success = hbaMapper.insert(record);
        return success;
    }

    @Override
    public ArrayList<HbaOCM> allOCM() {
        ArrayList<HbaOCM> hbaOCMS = hbaMapper.allOCM();
        return hbaOCMS;
    }

    @Override
    public int del() {
        int count = hbaMapper.del();
        return count;
    }


}
